package me.ingxin.android.keyboard.view

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.TextView
import me.ingxin.android.keyboard.entry.Key
import me.ingxin.android.tools.dp2px

/**
 * Created by ingxin on 2023/2/6
 * 安全键盘view
 */
class SafeKeyboardView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : LinearLayout(context, attrs), IKeyboard {

    private val defaultThemeColor = Color.rgb(84, 166, 191)
    private val onKeyListenerProxy = IKeyboard.OnKeyListener { keyConfirm(it) }
    private lateinit var numberKeyboardView: NumberKeyboardView
    private lateinit var symbolKeyboardView: SymbolKeyboardView
    private lateinit var letterKeyboardView: LetterKeyboardView
    private var onKeyListener: IKeyboard.OnKeyListener? = null
    private var currentType: KeyboardType

    /**键盘标题*/
    lateinit var titleTextView: TextView
        private set

    /**完成*/
    lateinit var doneTextView: TextView
        private set

    /**切换键盘1*/
    lateinit var swTextView1: TextView
        private set

    /**切换键盘2*/
    lateinit var swTextView2: TextView
        private set

    /**分割线视图*/
    val lineView: View

    /**切换符号键盘提示文本*/
    var symbolText: String = "符"
        set(value) {
            field = value
            changeUI(currentType)
        }

    /**切换数字键盘提示文本*/
    var numberText: String = "123"
        set(value) {
            field = value
            changeUI(currentType)
        }

    /**切换字母键盘提示文本*/
    var letterText: String = "Abc"
        set(value) {
            field = value
            changeUI(currentType)
        }


    init {
        orientation = VERTICAL

        val titleView = createTitleView()
        addView(titleView, LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT))

        lineView = View(context)
        lineView.setBackgroundColor(Color.rgb(72, 72, 72))
        addView(lineView, LayoutParams(LayoutParams.MATCH_PARENT, context.dp2px(1.5f)))

        val keyboardContainer = createKeyboardContainer()
        addView(keyboardContainer, LayoutParams(LayoutParams.MATCH_PARENT, 0, 1f).apply {
            topMargin = context.dp2px(5f)
        })

        //默认显示字母键盘
        letterKeyboardView.visibility = View.VISIBLE
        currentType = KeyboardType.Letter
        setThemeColor(defaultThemeColor)
        changeUI(currentType)

        doneTextView.setOnClickListener { keyConfirm(Key(0f, 0f, 0f, 0f, Key.CODE_HIDE, "完成")) }
        swTextView1.setOnClickListener {
            val text = swTextView1.text?.toString()
            switchKeyboard(text)
        }
        swTextView2.setOnClickListener {
            val text = swTextView2.text?.toString()
            switchKeyboard(text)
        }
    }

    private fun switchKeyboard(text: String?) {
        if (text?.isNotEmpty() == true) {
            currentType = when (text) {
                symbolText -> KeyboardType.Symbol
                letterText -> KeyboardType.Letter
                numberText -> KeyboardType.Number
                else -> return
            }
            changeUI(currentType)
        }
    }

    private fun changeUI(type: KeyboardType) {
        when (type) {
            KeyboardType.Letter -> {
                numberKeyboardView.visibility = View.GONE
                symbolKeyboardView.visibility = View.GONE
                letterKeyboardView.visibility = View.VISIBLE
                swTextView1.text = symbolText
                swTextView2.text = numberText
            }

            KeyboardType.Symbol -> {
                numberKeyboardView.visibility = View.GONE
                symbolKeyboardView.visibility = View.VISIBLE
                letterKeyboardView.visibility = View.GONE
                swTextView1.text = numberText
                swTextView2.text = letterText
            }

            KeyboardType.Number -> {
                numberKeyboardView.visibility = View.VISIBLE
                symbolKeyboardView.visibility = View.GONE
                letterKeyboardView.visibility = View.GONE
                swTextView1.text = symbolText
                swTextView2.text = letterText
            }
        }
    }

    private fun createTitleView(): ViewGroup {
        val defaultTextSize = 16f

        val container = FrameLayout(context)

        //标题
        titleTextView = TextView(context).apply {
            setTextSize(TypedValue.COMPLEX_UNIT_DIP, defaultTextSize)
            val paddingV = context.dp2px(6f)
            setPadding(0, paddingV, 0, paddingV)
            setTextColor(Color.WHITE)
        }
        container.addView(titleTextView, FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT
        ).apply { gravity = Gravity.CENTER })

        val paddingH = context.dp2px(20f)

        //完成
        doneTextView = TextView(context).apply {
            text = "完成"
            setTextSize(TypedValue.COMPLEX_UNIT_DIP, defaultTextSize)
            setPadding(paddingH, 0, paddingH, 0)
        }
        container.addView(doneTextView, FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT
        ).apply { gravity = Gravity.CENTER_VERTICAL or Gravity.END })

        //切换键盘
        val swGroup = LinearLayout(context).apply { orientation = HORIZONTAL }

        //切换键盘1
        swTextView1 = TextView(context).apply {
            setTextSize(TypedValue.COMPLEX_UNIT_DIP, defaultTextSize)
            setPadding(paddingH, 0, paddingH, 0)
        }
        swGroup.addView(
            swTextView1,
            LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)
        )

        //切换键盘2
        swTextView2 = TextView(context).apply {
            setTextSize(TypedValue.COMPLEX_UNIT_DIP, defaultTextSize)
            setPadding(0, 0, paddingH, 0)
        }
        swGroup.addView(
            swTextView2,
            LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)
        )

        container.addView(swGroup, FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.MATCH_PARENT
        ).apply { gravity = Gravity.CENTER_VERTICAL or Gravity.START })

        return container
    }

    private fun createKeyboardContainer(): ViewGroup {
        val keyboardContainer = FrameLayout(context)
        numberKeyboardView = NumberKeyboardView(context)
        symbolKeyboardView = SymbolKeyboardView(context)
        letterKeyboardView = LetterKeyboardView(context)
        val lp = FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT
        )
        allKeyboardView().forEach {
            it.setOnKeyListener(onKeyListenerProxy)
            keyboardContainer.addView(it, lp)
            it.visibility = View.GONE
        }
        return keyboardContainer
    }

    private fun keyConfirm(key: Key) {
        when (key.code) {
            Key.CODE_SHIFT_LOWER, Key.CODE_SHIFT_UP, Key.CODE_LETTER, Key.CODE_SYMBOL, Key.CODE_NUMBER -> {
                //do nothing
            }
            else -> onKeyListener?.onKey(key)
        }
    }

    override fun setOnKeyListener(listener: IKeyboard.OnKeyListener) {
        this.onKeyListener = listener
    }

    override fun setKeyIcon(keyCode: Int, drawable: Drawable?) {
        allKeyboardView().forEach { it.setKeyIcon(keyCode, drawable) }
    }

    override fun setThemeColor(color: Int) {
        doneTextView.setTextColor(color)
        swTextView1.setTextColor(color)
        swTextView2.setTextColor(color)
        allKeyboardView().forEach { it.setThemeColor(color) }
    }

    override fun setKeyBackground(drawable: Drawable?) {
        allKeyboardView().forEach { it.setKeyBackground(drawable) }
    }

    override fun setHorizontalGap(gap: Float) {
        allKeyboardView().forEach { it.setHorizontalGap(gap) }
    }

    override fun setVerticalGap(gap: Float) {
        allKeyboardView().forEach { it.setVerticalGap(gap) }
    }

    override fun setKeyTextSize(size: Float) {
        allKeyboardView().forEach { it.setKeyTextSize(size) }
    }

    override fun setKeyTextColor(color: Int) {
        allKeyboardView().forEach { it.setKeyTextColor(color) }
    }

    override fun resetUI() {
        allKeyboardView().forEach { it.resetUI() }
    }

    private fun allKeyboardView() =
        listOf(numberKeyboardView, symbolKeyboardView, letterKeyboardView)

    private enum class KeyboardType {
        /**字母键盘*/
        Letter,

        /**数字键盘*/
        Number,

        /**符号键盘*/
        Symbol
    }

}